//
//  ReserveController.m
//  SimpleHappy
//
//  Created by lanou on 15/11/23.
//  Copyright © 2015年 RockyFung. All rights reserved.
//

#import "ReserveController.h"
#import "FoodTableViewController.h"
#import "PlayTableViewController.h"
#import "LiveTableViewController.h"
#import "PlaceViewController.h"
#import "Define.h"
#import "FieldHeader.h"
#import "PictureModel.h"
@interface ReserveController ()<UIScrollViewDelegate,UIAlertViewDelegate>
@property(nonatomic,retain)UIButton *placeButton;//显示地方的button
@property(nonatomic,retain)UIButton *imageButton;//显示图标的button
@property(nonatomic,retain)UIButton *screening;//筛选
@property(nonatomic,retain)UISegmentedControl *segment;
@property(nonatomic,retain)UIScrollView *scrollView;
@property(nonatomic,retain)FoodTableViewController *foodVc;
@property(nonatomic,retain)PlayTableViewController *playVc;
@property(nonatomic,retain)LiveTableViewController *liveVc;
@property(nonatomic,retain)NSMutableArray *reserveArray;//装数据的数组
@property(nonatomic,retain)UIView *bgView;

@end

@implementation ReserveController
- (void)viewWillAppear:(BOOL)animated
{
    NSString *name = [[NSUserDefaults standardUserDefaults]objectForKey:@"name"];
    if (![self.cityModel.name isEqualToString:name])
    {
        if (self.cityModel.name==nil)
        {
            [self viewDidLoad];
        }
        else
        {
            self.cityModel.name = name;
            [self.placeButton setTitle:name forState:UIControlStateNormal];
            [self.segment removeFromSuperview];
            [self addSegment];
        }
    }
}
- (void)viewDidLoad {
    [super viewDidLoad];
    //设置navigationbar
//    self.navigationController.hidesBarsOnSwipe = YES;
    self.navigationController.navigationBar.translucent = NO;
    
    
    // 标题格式
    UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, KScreenWidth / 7.5, KScreenWidth / 4.68)];
    titleLabel.text = @"SimpleHappy";
    titleLabel.font = [UIFont fontWithName:@"SnellRoundhand-Bold" size:KScreenWidth / 18.75];
    self.navigationItem.titleView = titleLabel;
    
    
    self.placeButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.placeButton.frame = CGRectMake(0, 0, 50*KScreenWidth/375, 30*KScreenWidth/375);
    [self.placeButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    self.cityModel = [[CityModel alloc]init];
    self.cityModel.name = [[NSUserDefaults standardUserDefaults]objectForKey:@"name"];
    if (self.cityModel.name)
    {
        [self.placeButton setTitle:self.cityModel.name forState:UIControlStateNormal];
    }
    else
    {
        [self placeButtonAction];
        return;
    }
    [self.placeButton addTarget:self action:@selector(placeButtonAction) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *leftButton1 = [[UIBarButtonItem alloc]initWithCustomView:self.placeButton];
    
    self.imageButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.imageButton.frame = CGRectMake(0, 0, 30*KScreenWidth/375, 30*KScreenWidth/375);
    [self.imageButton setImage:[UIImage imageNamed:@"xia"] forState:UIControlStateNormal];
    [self.imageButton addTarget:self action:@selector(placeButtonAction) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *leftButton2 = [[UIBarButtonItem alloc]initWithCustomView:self.imageButton];
    self.navigationItem.leftBarButtonItems=@[leftButton1,leftButton2];

    self.navigationController.navigationBar.translucent = NO;
    [self addSegment];
}
#pragma mark-----------添加segment的按钮---------------
-(void)addSegment
{
    CGFloat height=30*KScreenWidth/375;
    if ([self.cityModel.name isEqualToString:@"重庆"]||[self.cityModel.name isEqualToString:@"天津"])
    {
        NSArray *array = @[@"美食",@"玩乐"];
        self.segment = [[UISegmentedControl alloc]initWithItems:array];
        self.segment.frame = CGRectMake((KScreenWidth- KScreenWidth/1.2) / 2, 0, KScreenWidth/1.2, height);
        self.segment.selectedSegmentIndex = 0;
        //给segment按钮添加背景颜色
        self.bgView = [[UIView alloc]initWithFrame:CGRectMake(10, height-2, self.segment.frame.size.width/2-20, 2)];
        self.bgView.backgroundColor = [UIColor grayColor];
      
        [self.segment addSubview:_bgView];
        //segment添加点击触发方法
        [self.segment addTarget:self action:@selector(segmentActionWithoutLiveOrFood:) forControlEvents:UIControlEventValueChanged];
        [self.view addSubview:self.segment];
        self.segment.tintColor = [UIColor clearColor];//去掉颜色,现在整个segment都看不见
        NSDictionary* selectedTextAttributes = @{NSFontAttributeName:[UIFont boldSystemFontOfSize:16],
                                                 NSForegroundColorAttributeName: [UIColor blackColor]};
        [self.segment setTitleTextAttributes:selectedTextAttributes forState:UIControlStateSelected];//设置文字属性
        NSDictionary* unselectedTextAttributes = @{NSFontAttributeName:[UIFont boldSystemFontOfSize:16],
                                                   NSForegroundColorAttributeName: [UIColor grayColor]};
        [self.segment setTitleTextAttributes:unselectedTextAttributes forState:UIControlStateNormal];
        //初始化scrollview
        self.scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, height, KScreenWidth, KScreenHeight)];
        self.scrollView.contentSize=CGSizeMake(KScreenWidth*2, KScreenHeight);
        self.scrollView.contentOffset = CGPointMake(0, 0);
        self.scrollView.pagingEnabled = YES;
        self.scrollView.bounces=NO;
        [self.view addSubview:self.scrollView];
        self.scrollView.delegate = self;
        //添加第一个界面美食的tableview
        self.foodVc = [[FoodTableViewController alloc]initWithStyle:UITableViewStyleGrouped];
        self.foodVc.view.frame = CGRectMake(0, 0, KScreenWidth, KScreenHeight);
        [self.scrollView addSubview:_foodVc.view];
        [self addChildViewController:self.foodVc];
        //添加第二个界面玩乐的tableview
        self.playVc = [[PlayTableViewController alloc]initWithStyle:UITableViewStyleGrouped];
        self.playVc.view.frame = CGRectMake(KScreenWidth, 0, KScreenWidth, KScreenHeight);
        [self.scrollView addSubview:_playVc.view];
        [self addChildViewController:self.playVc];
    }
    else
        if ([self.cityModel.name isEqualToString:@"厦门"]||[self.cityModel.name isEqualToString:@"南京"])
        {
            NSArray *array = @[@"玩乐",@"生活学院"];
            self.segment = [[UISegmentedControl alloc]initWithItems:array];
            self.segment.frame = CGRectMake((KScreenWidth- KScreenWidth/1.2) / 2, 0, KScreenWidth/1.2, height);
            self.segment.selectedSegmentIndex = 0;
            //给segment按钮添加背景颜色
            self.bgView = [[UIView alloc]initWithFrame:CGRectMake(10, height-2, self.segment.frame.size.width/2-20, 2)];
            self.bgView.backgroundColor = [UIColor grayColor];
//
            [self.segment addSubview:_bgView];
            
            //segment添加点击触发方法
            [self.segment addTarget:self action:@selector(segmentActionWithoutLiveOrFood:) forControlEvents:UIControlEventValueChanged];
            [self.view addSubview:self.segment];
            self.segment.tintColor = [UIColor clearColor];//去掉颜色,现在整个segment都看不见
            NSDictionary* selectedTextAttributes = @{NSFontAttributeName:[UIFont boldSystemFontOfSize:16],
                                                     NSForegroundColorAttributeName: [UIColor blackColor]};
            [self.segment setTitleTextAttributes:selectedTextAttributes forState:UIControlStateSelected];//设置文字属性
            NSDictionary* unselectedTextAttributes = @{NSFontAttributeName:[UIFont boldSystemFontOfSize:16],
                                                       NSForegroundColorAttributeName: [UIColor grayColor]};
            [self.segment setTitleTextAttributes:unselectedTextAttributes forState:UIControlStateNormal];
            //初始化scrollview
            self.scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, height, KScreenWidth, KScreenHeight)];
            self.scrollView.contentSize=CGSizeMake(KScreenWidth*2, KScreenHeight);
            self.scrollView.contentOffset = CGPointMake(0, 0);
            self.scrollView.pagingEnabled = YES;
            self.scrollView.bounces=NO;
            
            
            
            [self.view addSubview:self.scrollView];
            self.scrollView.delegate = self;
            //添加第二个界面玩乐的tableview
            self.playVc = [[PlayTableViewController alloc]initWithStyle:UITableViewStyleGrouped];
            self.playVc.view.frame = CGRectMake(0, 0, KScreenWidth, KScreenHeight);
            [self.scrollView addSubview:_playVc.view];
            [self addChildViewController:self.playVc];
            //添加第三个界面生活学院的tableview
            self.liveVc = [[LiveTableViewController alloc]initWithStyle:UITableViewStyleGrouped];
            self.liveVc.view.frame = CGRectMake(KScreenWidth, 0, KScreenWidth, KScreenHeight);
            [self.scrollView addSubview:_liveVc.view];
            [self addChildViewController:self.liveVc];
    }
        else
        {
            NSArray *array = @[@"美食",@"玩乐",@"生活学院"];
            self.segment = [[UISegmentedControl alloc]initWithItems:array];
            self.segment.frame = CGRectMake((KScreenWidth- KScreenWidth/1.2) / 2, 0, KScreenWidth/1.2, height);
            self.segment.selectedSegmentIndex = 0;
            //给segment按钮添加背景颜色
            self.bgView = [[UIView alloc]initWithFrame:CGRectMake(10, height - 2, self.segment.frame.size.width/3-20, 2)];
            self.bgView.backgroundColor = [UIColor grayColor];

            [self.segment addSubview:_bgView];
        //segment添加点击触发方法
        [self.segment addTarget:self action:@selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
        [self.view addSubview:self.segment];
        self.segment.tintColor = [UIColor clearColor];//去掉颜色,现在整个segment都看不见
        NSDictionary* selectedTextAttributes = @{NSFontAttributeName:[UIFont boldSystemFontOfSize:16],
            NSForegroundColorAttributeName: [UIColor blackColor]};
        [self.segment setTitleTextAttributes:selectedTextAttributes forState:UIControlStateSelected];//设置文字属性
        NSDictionary* unselectedTextAttributes = @{NSFontAttributeName:[UIFont boldSystemFontOfSize:16],
        NSForegroundColorAttributeName: [UIColor grayColor]};
        [self.segment setTitleTextAttributes:unselectedTextAttributes forState:UIControlStateNormal];
        
        //初始化scrollview
        self.scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, height, KScreenWidth, KScreenHeight)];
        self.scrollView.contentSize=CGSizeMake(KScreenWidth*3, KScreenHeight);
        self.scrollView.contentOffset = CGPointMake(0, 0);
        self.scrollView.pagingEnabled = YES;
        self.scrollView.bounces=NO;

        [self.view addSubview:self.scrollView];
        self.scrollView.delegate = self;
        //添加第一个界面美食的tableview
        self.foodVc = [[FoodTableViewController alloc]initWithStyle:UITableViewStyleGrouped];
        self.foodVc.view.frame = CGRectMake(0, 0, KScreenWidth, KScreenHeight);
        [self.scrollView addSubview:_foodVc.view];
        [self addChildViewController:self.foodVc];
        //添加第二个界面玩乐的tableview
        self.playVc = [[PlayTableViewController alloc]initWithStyle:UITableViewStyleGrouped];
        self.playVc.view.frame = CGRectMake(KScreenWidth, 0, KScreenWidth, KScreenHeight);
        [self.scrollView addSubview:_playVc.view];
        [self addChildViewController:self.playVc];
        //添加第三个界面生活学院的tableview
        self.liveVc = [[LiveTableViewController alloc]initWithStyle:UITableViewStyleGrouped];
        self.liveVc.view.frame = CGRectMake(KScreenWidth*2, 0, KScreenWidth, KScreenHeight);
        [self.scrollView addSubview:_liveVc.view];
        [self addChildViewController:self.liveVc];
    }
}
#pragma mark-----------scrollView的触发方法------------
//没有生活类或者美食类的segment
-(void)segmentActionWithoutLiveOrFood:(UISegmentedControl *)segment
{
    CGFloat height = 30*KScreenWidth/375;
    NSInteger i = segment.selectedSegmentIndex;
    self.bgView.frame =CGRectMake((segment.frame.size.width/2)*i+10, height-2, segment.frame.size.width/2-20, 2);
    [self.scrollView setContentOffset:CGPointMake(KScreenWidth*i, 0) animated:YES];
}
//三类全有
-(void)segmentAction:(UISegmentedControl *)segment
{
    CGFloat height = 30*KScreenWidth/375;
    NSInteger i = segment.selectedSegmentIndex;
    self.bgView.frame =CGRectMake((segment.frame.size.width/3)*i+10, height-2, segment.frame.size.width/3-20, 2);
    [self.scrollView setContentOffset:CGPointMake(KScreenWidth*i, 0) animated:YES];
}
#pragma mark----------scrollView的代理方法----------
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    CGFloat height = 30*KScreenWidth/375;
    CGPoint point = scrollView.contentOffset;
    self.segment.selectedSegmentIndex = point.x/KScreenWidth;
    if ([self.cityModel.name isEqualToString:@"南京"]||[self.cityModel.name isEqualToString:@"厦门"]||[self.cityModel.name isEqualToString:@"重庆"]||[self.cityModel.name isEqualToString:@"天津"])
    {
        self.bgView.frame = CGRectMake(((self.segment.frame.size.width/2)*self.segment.selectedSegmentIndex)+10, height-2, self.segment.frame.size.width/2-20, 2);
    }
    else
    {
        self.bgView.frame = CGRectMake(((self.segment.frame.size.width/3)*self.segment.selectedSegmentIndex)+10, height-2, self.segment.frame.size.width/3-20, 2);
    }
}
#pragma mark---------左边按钮的触发方法-----------------
-(void)placeButtonAction
{
    PlaceViewController *place = [[PlaceViewController alloc]init];
    
    [self.navigationController presentViewController:place animated:YES completion:nil];
}






@end











